/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.assg2.dao;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;


/**
 *
 * @author alexnewski
 */
import org.hibernate.Session;
import org.hibernate.context.ManagedSessionContext;
import org.hibernate.criterion.Restrictions;

public abstract class GenericDao {

    protected Class persistedClass;
    protected Session session;

    public GenericDao(Class persistedClass) {
        this.session = HibernateUtil.currentSession();
        this.persistedClass = persistedClass;
    }

    protected Criteria criteria() {
        return session.createCriteria(persistedClass);
    }

    protected Query query(String hql) {
        return session.createQuery(hql);
    }

    public void save(Object object) {
        try {
            session.beginTransaction();
            session.save(object);
            session.getTransaction().commit();


        } catch (Throwable e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }

            System.out.println(e.getMessage());
        }
    }

    public void savemiddletable(Object object) {
        try {
            session.beginTransaction();
            session.save(object);

        } catch (Throwable e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }

            System.out.println(e.getMessage());
        }
    }
    
    
    public void updatemiddletable(Object object) {
        try {
            session.beginTransaction();
            session.update(object);

        } catch (Throwable e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }

            System.out.println(e.getMessage());
        }
    }

    public void commit() {

        session.flush();
        session.getTransaction().commit();
 
    }

    public Object findById(int id) {
        Object object = (Object) session.get(persistedClass, id);
        return object;
    }

    public void remove(Object object) {

        try {
            session.beginTransaction();
            session.delete(object);
            session.getTransaction().commit();

        } catch (Exception e) {
            System.out.println("Exception occurs during delete a " + persistedClass.getName());
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
    }

    public void update(Object object) {

        try {
            session.beginTransaction();
            session.update(object);
            session.getTransaction().commit();


        } catch (Exception e) {
            System.out.println("Exception occurs when update a " + persistedClass.getName());
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
    }

    public List listall() {
        return criteria().list();
    }

    public Object findBy(String thisfield, String string) {
        return (Object) criteria().add(Restrictions.eq(thisfield, string)).uniqueResult();
    }
    
    
}
